package sniff.diller;

import jpcap.packet.IPPacket;
import jpcap.packet.Packet;
import sniff.pack.MyICMPPacket;
import util.ByteUtil;
import util.InfoUtil;

public class ICMPPacketDiller {
	public static MyICMPPacket dill(Packet pack) {
		byte[] head = pack.header;
		int startIndex=34;
		if(((IPPacket) pack).version == 4) {
			startIndex=34;
		}else if(((IPPacket) pack).version == 6){
			startIndex=54;
		}else {
			System.out.println("!!!!!!!!!!!!!!!error ICMPPacketDiller");
			return null;
		}
		int icmpType = ByteUtil.byteToInt(head[startIndex]);
		int code = ByteUtil.byteToInt(head[startIndex+1]);
		String checkSum = "0x"+ByteUtil.byteToHexString(head[startIndex+2])+ByteUtil.byteToHexString(head[startIndex+3]);
		int identifierBE = ByteUtil.unionByte(head[startIndex+4], head[startIndex+5]);
		byte b2=0;
		int identifierLE = ByteUtil.unionByte(head[startIndex+5], b2);
		int seqNumBE = ByteUtil.unionByte(head[startIndex+6], head[startIndex+7]);
		int seqNumLE = ByteUtil.unionByte(head[startIndex+7], b2);
		MyICMPPacket icmpPack = new MyICMPPacket(icmpType, code, checkSum, identifierBE, identifierLE, seqNumBE, seqNumLE);
		icmpPack.setInfo(InfoUtil.getICMPInfo(icmpPack));
		return icmpPack;
	}
}
